/*
 * Copyright (C) Francisco Morero Peyrona. All rights reserved.
 *
 * This software is published under the terms of Open Source
 * License version 1.1, a copy of which has been included with this
 * distribution in the License.txt file.
 */

package jdbcmanager.diagram;

import javax.swing.*;
import java.awt.*;
import jdbcmanager.swing.*;
import java.awt.event.*;

/**
 * Dialogs to add/remove tables to/from diagram. 
 *
 * @author Francisco Morero Peyrona
 */

public final class SelectTableDlg extends JxDialog
{
   private JButton     btnSelectAll     = new JButton();
   private JButton     btnSelectNone    = new JButton();
   private JPanel      pnlCheckList     = new JPanel();
   private JScrollPane spCheckList      = new JScrollPane();
   private JCheckList  chklstTables     = new JCheckList();
   private JCheckBox   chkShowAllTables = new JCheckBox(); 

   private String[]    asSeleceted   = new String[0];  // Selected tables names

   //-------------------------------------------------------------------------//

   public SelectTableDlg()
   {
      super( "Add/Remove tables to/from diagram" );

      init();
      pack( 0, -400 );
   }

   public void addItem( String sItem, boolean bSelected )
   {
      this.chklstTables.add( sItem, bSelected );

      if( this.chklstTables.getSelectedIndex() > -1 )
         this.chklstTables.setSelectedIndex( 0 );
   }

   public String[] getSelecetdTables()
   {
      return this.asSeleceted;
   }

   //-------------------------------------------------------------------------//

   protected void onAccept( ActionEvent evt )
   {
      JCheckBox[] aCheck = this.chklstTables.getSelectedChecks();

      this.asSeleceted = new String[ aCheck.length ];

      for( int n = 0; n < aCheck.length; n++ )
         this.asSeleceted[n] = aCheck[n].getText();

      super.onAccept( evt );
   }

   private void onSelectAll()
   {
      this.chklstTables.selectAll();
   }

   private void onSelectNone()
   {
      this.chklstTables.deselectAll();
   }
   
   /**
    * Shows tables for all types or only tables of type "TABLE" 
    * 
    * @param bAll If <code>true</code> shows tables for all types; otherwise, only tables of type "TABLE"
    */
   private void onShowAllTables( boolean bAll )
   {
      /* NEXT Show all tables or only those of type "TABLE"
              pnlCheckList.add( this.chkShowAllTables, BorderLayout.SOUTH  );
      */
   }
   
   private void init()
   {
      btnSelectAll.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent e )
         {
            onSelectAll();
         }
      } );

      btnSelectNone.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent e )
         {
            onSelectNone();
         }
      } );


      JLabel lblTip = new JLabel( "  Use double-click to select/deselect items" );

      chklstTables.setToolTipText( "Double-Click mark/unmark selected item" );
      spCheckList.getViewport().add( chklstTables, null );

      pnlCheckList.setBorder( BorderFactory.createEmptyBorder( 15, 15, 15, 15 ) );
      pnlCheckList.setLayout( new BorderLayout() );
      pnlCheckList.add( spCheckList, BorderLayout.CENTER );
      
      this.chkShowAllTables.setText( "Show all tables" );
      this.chkShowAllTables.addActionListener( new ActionListener()
      {
         public void actionPerformed( ActionEvent evt )
         {
            onShowAllTables( chkShowAllTables.isSelected() );
         }
      } );
                  
      // Panel Buttons ------------------------//
      btnSelectAll.setText( "All" );
      btnSelectAll.setMnemonic( 'l' );
      btnSelectNone.setText( "None" );
      btnSelectNone.setMnemonic( 'N' );

      pnlButtons.removeAll();
      pnlButtons.add( btnSelectAll );
      pnlButtons.add( btnSelectNone );
      pnlButtons.add( btnAccept );
      pnlButtons.add( btnCancel );
      //---------------------------------------//
      
      getContentPane().add( lblTip   , BorderLayout.NORTH  );
      getContentPane().add( pnlCheckList, BorderLayout.CENTER );
   }
}